package com.tensai.study.java.newconcept.typereactiveprogram.reactor.flux;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Function;

/**
 * @author Tensai
 */
public class RepeatTest {

	private final MyClass myClass = new MyClass();

	/**
	 * 只是重新订阅 源没有重新生成
	 */
	@Test
	public void test1(){
		myClass.myMethod()
				.repeat()
				.blockLast();
	}
	@Test
	public void test2(){
		Mono.defer(myClass::myMethod)
				.repeat()
				.blockLast();
	}
	@Test
	public void test3(){
		Flux.defer(myClass::myFluxMethod)
				.repeat()
				.blockLast();
	}

	/**
	 * 永远重复订阅
	 */
	@Test
	public void test4(){
		Mono.fromSupplier(myClass::myMethod)
				.flatMap(Function.identity())
				.repeat()
				.blockLast();
	}

	/**
	 * 重复十次
	 */
	@Test
	public void test5(){
		Mono.fromSupplier(myClass::myMethod)
				.flatMap(Function.identity())
				.repeat(10)
				.blockLast();
	}

	public static class MyClass {

		private Mono<String> myMethod() {
			System.out.println("execute myMethod");

			return Mono.just("Value");
		}
		private Flux<String> myFluxMethod() {
			System.out.println("execute myMethod");

			return Flux.just("Value");
		}
	}
}
